/*
 * The MIT License
 *
 * Copyright (c) 2010, Marc Schwerdtfeger (marc@schwerdtfeger.de)
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package org.loosix.core.modules.logger.impl
{
import org.loosix.core.module.constant.ModuleState;
import org.loosix.core.module.impl.AbstractModule;
import org.loosix.core.modules.logger.*;
import org.loosix.core.utils.ModuleUtil;

public class LoggerBasicImpl extends AbstractModule implements LoggerService
    {
        public static const LEVEL_DEBUG:int = 2;
        public static const LEVEL_INFO:int = 3;
        public static const LEVEL_LOG:int = 5;
        public static const LEVEL_WARN:int = 7;
        public static const LEVEL_ERROR:int = 8;
        public static const LEVEL_FATAL:int = 9;

        private static var _level:int = 0;

        public function LoggerBasicImpl()
        {
            super(ModuleUtil.getInterfaceName(LoggerService),"Loosix Basic Tracer")
            info("LoosixBasicLogger: loglevel = " + _level, this);                             
        } // endConstructor

        public function log(message:String, target:Object, object:Object = null):void
        {
            if (_level <= LEVEL_LOG) sendTrace(LEVEL_LOG, message, target, object);
        } // endFunc

        public function info(message:String, target:Object, object:Object = null):void
        {
            if (_level <= LEVEL_INFO) sendTrace(LEVEL_INFO, message, target, object);
        } // endFunc

        public function debug(message:String, target:Object, object:Object = null):void
        {
            if (_level <= LEVEL_DEBUG) sendTrace(LEVEL_DEBUG, message, target, object);
        } // endFunc

        public function warn(message:String, target:Object, object:Object = null):void
        {
            if (_level <= LEVEL_WARN) sendTrace(LEVEL_WARN, message, target, object);
        } // endFunc

        public function error(message:String, target:Object, object:Object = null):void
        {
            if (_level <= LEVEL_LOG) sendTrace(LEVEL_ERROR, message, target, object);
        } // endFunc

        public function fatal(message:String, target:Object, object:Object = null):void
        {
            if (_level <= LEVEL_FATAL) sendTrace(LEVEL_FATAL, message, target, object);
        } // endFunc

        private function sendTrace(level:int, message:String, target:Object, object:Object = null):void
        {
            if(state != ModuleState.STARTED) return ;
            
            var targetName:String = formatTargetTo(target);
            if (object) trace(levelToString(level) + "->  " + targetName + ": '" + message + "' Object=" + object);
            else trace(levelToString(level) + "->  " + targetName + ": '" + message);
        } // endFunc

        private static function formatTargetTo(target:Object):String
		{
            if(target is String) return target as String;
            var className:String = ModuleUtil.getClassName(target);
            
			var packageArray:Array = className.split(".");
			return packageArray[packageArray.length-1];
		} // endFunc

        public function set level(zeroTo10:uint):void
        {
            _level = Math.min(10, zeroTo10);
        } // endFunc

        public function get level():uint
        {
            return _level;
        } // endFunc

        private function levelToString(level:uint):String
        {
            switch (level)
            {
                case 2:
                    return "DEBUG";
                    break;
                case 3:
                    return "INFO";
                    break;
                case 5:
                    return "LOG";
                    break;
                case 7:
                    return "WARN";
                    break;
                case 8:
                    return "ERROR";
                    break;
                case 9:
                    return "FATAL";
                    break;
            } // endSwitch
            return "UNDEFINED_LEVEL";
        } // endFunc

        override public function toString():String
        {
            return "org.loosix.core.modules.logger.impl.LoggerBasicImpl";
        } // endFunc

    } // endClass

} // endPackage